<?php

namespace app\api\model\user;

use app\common\model\settings\Region;
use app\common\model\user\RegisterInfo as RegisterInfoModel;

/**
 * 用户收货地址模型
 */
class RegisterInfo extends RegisterInfoModel
{
    /**
     * 隐藏字段
     */
    protected $hidden = [
        'app_id',
        'create_time',
        'update_time'
    ];

    /**
     * 获取列表
     */
    public function getList($user_id)
    {
        return $this->where('user_id', '=', $user_id)->select();
    }

    /**
     * 新增收货地址
     */
    public function add($user, $data)
    {
        $check = $this->where(array('user_id'=>$user['user_id']))->find();
        if($check){
            $sldata = [
                'province_id'      => $data['province_id'],
                'city_id'          => $data['city_id'],
                'region_id'        => $data['region_id'],
                'city_label'       => $data['city_label'],
                'idcard_pic1'      => $data['idcard_pic1'],
                'idcard_pic2'      => $data['idcard_pic2'],
                'driving_license1' => $data['driving_license1'],
                'driving_license2' => $data['driving_license2'],
                'car_info1'        => $data['car_info1'],
                'car_info2'        => $data['car_info2'],
                'car_pic'          => $data['car_pic'],
                'remark'          => $data['remark'],
                'driver_name'       =>$data['driver_name'],
                'driver_phone'       =>$data['driver_phone'],
                'app_id'           => self::$app_id,
                'status'    =>0
            ];
            $id = $this->where(array('id'=>$check['id']))->update($sldata);

        }else{
            $id = $this->insertGetId([
                'province_id'      => $data['province_id'],
                'city_id'          => $data['city_id'],
                'region_id'        => $data['region_id'],
                'city_label'       => $data['city_label'],
                'idcard_pic1'      => $data['idcard_pic1'],
                'idcard_pic2'      => $data['idcard_pic2'],
                'driving_license1' => $data['driving_license1'],
                'driving_license2' => $data['driving_license2'],
                'car_info1'        => $data['car_info1'],
                'car_info2'        => $data['car_info2'],
                'car_pic'          => $data['car_pic'],
                'remark'          => $data['remark'],
                'user_id'          => $user['user_id'],
                'create_time'       => time(),
                'driver_name'       =>$data['driver_name'],
                'driver_phone'       =>$data['driver_phone'],
                'app_id'           => self::$app_id
            ]);
        }


        return $id;
    }

    /**
     * 编辑收货地址
     */
    public function edit($data)
    {
        // 添加收货地址
        $region = explode(',', $data['region']);
        $province_id = Region::getIdByName($region[0], 1);
        $city_id = Region::getIdByName($region[1], 2, $province_id);
        $region_id = Region::getIdByName($region[2], 3, $city_id);
        return $this->save([
                'name' => $data['name'],
                'phone' => $data['phone'],
                'province_id' => $province_id,
                'city_id' => $city_id,
                'region_id' => $region_id,
                'detail' => $data['detail'],
                'district' => ($region_id === 0 && !empty($region[2])) ? $region[2] : '',
            ]) !== false;
    }

    /**
     * 收货地址详情
     */
    public static function detail($user_id, $id)
    {
        $where = ['user_id' => $user_id, 'id' => $id];
        return static::where($where)->find();
    }

}